package Server.Login.Controller;

import java.awt.*;
import java.awt.event.*;

import Server.Login.View.ServerLoginView;
import Server.Login.Model.ServerLoginModel;
import Server.Login.Controller.ServerLoginController;
import Exception.LoginFailureException;
import Exception.SocketConnectionFailureException;

public class LoginButtonListener implements ActionListener 
{
	public LoginButtonListener(ServerLoginView view, ServerLoginModel model, ServerLoginController controller)
	{
	    this.view = view;
	    this.model = model;
	    this.controller = controller;
	}
    public void actionPerformed(ActionEvent e)
    {
    	view.loginButton.setText(this.loginingMessage);
        try
        {
        	if (model.login(view.userNameTextField.getText(), view.passwordField.getPassword()))
            {
        	     view.loginStatusLabel.setText(this.loginSuccessfulMessage);
        	     view.frame.setVisible(false);
        	     controller.runMainFrame();
            }
        }
        catch(LoginFailureException exception)
        {
        	view.loginStatusLabel.setText(exception.toString());
        	System.out.println("LoginButton exception : LoginFailureException");
        	exception.printStackTrace();
        }
    }
    
    
    
    public ServerLoginView getView()
    {
        return this.view;	
    }
    
    public ServerLoginModel getModel()
    {
    	return this.model;
    }
    
    private ServerLoginView view;
    private ServerLoginModel model;
    private ServerLoginController controller;
    
    private String loginSuccessfulMessage = new String("Login Successful");
    private String loginingMessage = new String("Logining");
}
